package com.cxyzy.demo.tween_anims

import android.os.Bundle
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import androidx.appcompat.app.AppCompatActivity
import com.cxyzy.demo.R
import kotlinx.android.synthetic.main.activity_fade.*

/**
 * 动画样例：淡出淡入
 */
class FadeOutFadeInActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fade)
        initView()
    }

    private fun initView() {
        toAlphaBtn.setOnClickListener { fadeOutAndFadeIn(it) }
    }

        private fun fadeOutAndFadeIn(srcView: View) {
            val animateTime = 2000L
            val fadeOutAnimation = AlphaAnimation(1f, 0f)
            fadeOutAnimation.fillAfter = true
            fadeOutAnimation.duration = animateTime

            val fadeInAnimation = AlphaAnimation(0f, 1f)
            fadeInAnimation.fillAfter = true
            fadeInAnimation.duration = animateTime

            fadeOutAnimation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationEnd(animation: Animation) {
                    srcView.startAnimation(fadeInAnimation)
                }

                override fun onAnimationStart(animation: Animation) {}
                override fun onAnimationRepeat(animation: Animation) {}
            })

            srcView.startAnimation(fadeOutAnimation)
        }
}
